package com.gl.controller.user;

import com.gl.pojo.dto.*;
import com.gl.pojo.Result;
import com.gl.pojo.entity.User;
import com.gl.service.UserService;
import com.gl.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/users")
@CrossOrigin("*")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterRequest request) {
        UserRegisterResponse response = userService.register(request);
        return Result.success(response);
    }

    @PostMapping("login")
    public Result login(@RequestParam() String email, @RequestParam() String password) {
        UserLoginResponse response = userService.login(email, password);
        return Result.success(response);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/captcha")
    public Result captcha(@RequestParam() String email) {
        userService.captcha(email);
        return Result.success();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result info() {
        String userId = CurrentHolder.getCurrentId();
        User user = userService.selectById(userId);
        return Result.success(user);
    }

    /**
     * 分页查询用户信息
     */
    @GetMapping("/list")
    public Result listPage(UserPageDto userPage) {
        return Result.success(userService.page(userPage));
    }

    /**
     * 注销用户
     */
    @PostMapping("/users/delete")
    public Result deleteUser() {
        String userId = CurrentHolder.getCurrentId();
        userService.deleteById(userId);
        return Result.success();
    }

    /**
     * 更新用户名
     */
    @PostMapping("/update/username")
    public Result updateUsername(String username) {
        User user = new User();
        user.setId(CurrentHolder.getCurrentId());
        user.setUsername(username);
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 验证密码
     */
    @PostMapping("/identify/password")
    public Result identifyPassword(User user) {
        if (userService.identifyPassword(user)) return Result.success("密码正确");
        return Result.error("密码错误");
    }

    /**
     * 验证邮箱
     */
    public Result identifyCaptcha(String email, String captcha) {
        if (userService.identifyCaptcha(email, captcha)) return Result.success("验证码正确");
        return Result.error("验证码错误");
    }

    /**
     * 修改密码
     */
    @PostMapping("/update/password")
    public Result updatePassword(User user) {
        user.setId(CurrentHolder.getCurrentId());
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 修改头像
     */
    @PostMapping("/update/avatar")
    public Result updateAvatar(MultipartFile file) {
        userService.updateAvatar(file);
        return Result.success();
    }
}